﻿using System;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Plumbing;
using Autodesk.Revit.UI.Selection;

namespace YArchitech.Revit
{
	public class AutoDesignCommonPipe : ISelectionFilter
	{
		public AutoDesignCommonPipe(bool beDraining)
		{
			this.beDrainage = beDraining;
		}

		public bool AllowElement(Element elem)
		{
			if (elem is FamilyInstance)
			{
				FamilyInstance familyInstance = elem as FamilyInstance;
				BuiltInCategory integerValue = elem.Category.Id.IntegerValue;
				if (integerValue == -2008055 && (familyInstance.Symbol.Family.Name.Contains("地漏") || familyInstance.Symbol.Name.Contains("地漏")) && this.beDrainage)
				{
					return true;
				}
				if (integerValue == -2001160)
				{
					if (!this.beDrainage)
					{
						return true;
					}
					if (familyInstance.MEPModel.ConnectorManager != null)
					{
						return true;
					}
				}
			}
			if (elem is Pipe)
			{
				Pipe pipe = elem as Pipe;
				if (!YJKRevitTools.isRiserPipe(pipe))
				{
					PipeSystemType pipeSystemType = new YJKPiping<Pipe>(pipe).StartConnector.Connector.PipeSystemType;
					if (this.beDrainage && pipeSystemType == 16)
					{
						return true;
					}
					if (!this.beDrainage && (pipeSystemType == 20 || pipeSystemType == 19))
					{
						return true;
					}
				}
			}
			return false;
		}

		public bool AllowReference(Reference reference, XYZ position)
		{
			return false;
		}

		private bool beDrainage = true;
	}
}
